/**
 * Created by luoxiaoming on 16-12-25.
 */
Ext.define('console.deptbindpaper', {
    extend: 'Ext.ux.desktop.Module',
    requires: [
        'console.store.internalEvalMapperStore',
        'console.store.deptGroupStore',
        'console.store.testPaperStore',
        'Ext.data.ArrayStore',
        'Ext.util.Format',
        'Ext.grid.Panel',
        'Ext.grid.RowNumberer'
    ],

    id: 'deptbindpaper',
    init: function () {
        this.launcher = {
            text: '内部互评管理',
            iconCls: 'icon-grid'
        };
    },

    createWindow: function () {
        window.open("jsp/adminpaper.jsp");
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('internalEvalMapperWindow');
        if (!win) {
            win = desktop.createWindow({
                id: 'internalEvalMapperWindow',
                title: '内部互评管理',
                width: 800,
                height: 600,
                iconCls: 'icon-grid',
                animCollapse: false,
                constrainHeader: true,
                layout: 'fit',
                items: [
                    {
                        <!--'id','groupCode','groupName','targetGroupCode','targetGroupName','paperId','paperName','mark','weights'-->
                        border: false,
                        xtype: 'grid',
                        store: Ext.createWidget('internalEvalMapperStore'),
                        columns: [
                            {
                                text: "序号",
                                width: '8%',
                                sortable: true,
                                dataIndex: 'id'
                            }, {
                                text: "被评科室类别代码",
                                hidden: true,
                                sortable: true,
                                dataIndex: 'targetGroupCode'
                            }, {
                                text: "被评科室类别",
                                width: '20%',
                                sortable: true,
                                dataIndex: 'targetGroupName'
                            }, {
                                text: "评价科室类别代码",
                                hidden: true,
                                sortable: true,
                                dataIndex: 'groupCode'
                            }, {
                                text: "评价科室类别",
                                width: '20%',
                                sortable: true,
                                dataIndex: 'groupName'
                            }, {
                                text: "问卷",
                                width: '20%',
                                sortable: true,
                                dataIndex: 'paperName'
                            },
                            {
                                text: "权重/百分比",
                                width: '10%',
                                sortable: true,
                                dataIndex: 'weights'
                            },
                            {
                                text: "备注",
                                flex: 1,
                                sortable: true,
                                dataIndex: 'mark'
                            }
                        ]
                    }
                ],
                tbar: [{
                    text: '添加',
                    tooltip: '添加一行',
                    iconCls: 'add',
                    handler: this.add
                }, '-', {
                    text: '修改',
                    tooltip: '修改操作',
                    iconCls: 'option',
                    handler: this.option
                }, '-', {
                    text: '删除',
                    tooltip: '删除一组数据',
                    iconCls: 'remove',
                    handler: this.remove
                }, '-', {
                    text: '刷新',
                    tooltip: '刷新数据',
                    iconCls: 'refresh',
                    handler: this.refresh
                }]
            });
        }
        // return win;
    },

    add: function () {
        Ext.create('Ext.window.Window', {
            modal: true,
            items: [{
                title:'如添加临床科室类别，请注意权重/百分比',
                xtype: 'form',
                width: 400,
                defaults: {
                    labelAlign: 'left',
                    margin: '10 10 10 10',
                    width: '90%',
                    xtype: 'textfield',
                    labelWidth: 60
                },
                items: [{
                    xtype: 'combobox',
                    fieldLabel: '被评部门/科室类别',
                    name: 'targetGroupCode',
                    editable: false,
                    store: Ext.createWidget("deptGroupStore"),
                    queryMode: 'remote',
                    displayField: 'name',
                    valueField: 'code',
                    allowBlank: false
                }, {
                    xtype: 'combobox',
                    fieldLabel: '评价科室类别',
                    name: 'groupCode',
                    editable: false,
                    store: Ext.createWidget("deptGroupStore"),
                    queryMode: 'remote',
                    displayField: 'name',
                    valueField: 'code',
                    allowBlank: false
                }, {
                    xtype: 'combobox',
                    fieldLabel: '问卷',
                    name: 'paperId',
                    editable: false,
                    store: Ext.createWidget("testPaperStore"/*, {
                        listeners: {
                            beforeload: function (store, options) {
                                var extraParams = {type: 'A'};
                                Ext.apply(store.proxy.extraParams, extraParams);
                            }
                        }
                    }*/),
                    queryMode: 'remote',
                    displayField: 'name',
                    valueField: 'paperId',
                    allowBlank: false
                }, {
                    xtype: 'numberfield',
                    name: 'weights',
                    maxValue: 100,
                    minValue: 0,
                    fieldLabel: '权重/百分比',
                    allowBlank: false
                }, {
                    name: 'mark',
                    fieldLabel: '备注'
                }],
                buttons: [{
                    text: '重置',
                    handler: function () {
                        this.up('form').getForm().reset();
                    }
                }, {
                    text: '提交',
                    handler: function () {
                        var form = this.up('form').getForm();
                        if (form.isValid()) {
                            form.submit({
                                url: 'internalEval/addEval',
                                success: function (innerForm, action) {
                                    Ext.Msg.alert('', '添加成功', function () {
                                        form.owner.ownerCt.close();
                                        Ext.getCmp('internalEvalMapperWindow').query('grid')[0].store.reload();
                                    }, this);
                                },
                                failure: function (form, action) {
                                    var res = Ext.decode(action.response.responseText);
                                    Ext.Msg.alert('', res.failure);
                                }
                            });
                        }
                    }
                }]
            }]
        }).show();
    },

    option: function () {
        var row = this.ownerCt.ownerCt.query('grid')[0].getSelectionModel().selected.items;
        if (row.length == 0) {
            Ext.Msg.alert('', '需要选中一行记录');
            return;
        }
        var data = row[0].data;
        var win = Ext.create('Ext.window.Window', {
            modal: true,
            items: [{
                title:'如添加临床科室类别，请注意权重/百分比',
                xtype: 'form',
                width: 400,
                defaults: {
                    labelAlign: 'left',
                    margin: '10 10 10 10',
                    width: '90%',
                    xtype: 'textfield',
                    labelWidth: 60
                },
                items: [{
                    name: 'id',
                    fieldLabel: '序号',
                    readOnly:true
                },{
                    xtype: 'combobox',
                    fieldLabel: '被评部门/科室类别',
                    name: 'targetGroupCode',
                    editable: false,
                    store: Ext.createWidget("deptGroupStore"),
                    queryMode: 'remote',
                    displayField: 'name',
                    readOnly:true,
                    valueField: 'code',
                    allowBlank: false
                }, {
                    xtype: 'combobox',
                    fieldLabel: '评价科室类别',
                    name: 'groupCode',
                    editable: false,
                    readOnly:true,
                    store: Ext.createWidget("deptGroupStore"),
                    queryMode: 'remote',
                    displayField: 'name',
                    valueField: 'code',
                    allowBlank: false
                }, {
                    xtype: 'combobox',
                    fieldLabel: '问卷',
                    name: 'paperId',
                    editable: false,
                    store: Ext.createWidget("testPaperStore"/*, {
                        listeners: {
                            beforeload: function (store, options) {
                                var extraParams = {type: 'A'};
                                Ext.apply(store.proxy.extraParams, extraParams);
                            }
                        }
                    }*/  ),
                    queryMode: 'remote',
                    displayField: 'name',
                    valueField: 'paperId',
                    allowBlank: false
                }, {
                    xtype: 'numberfield',
                    name: 'weights',
                    maxValue: 99,
                    minValue: 0,
                    fieldLabel: '权重/百分比',
                    allowBlank: false
                }, {
                    name: 'mark',
                    fieldLabel: '备注'
                }],
                buttons: [{
                    text: '重置',
                    handler: function () {
                        this.up('form').getForm().reset();
                    }
                }, {
                    text: '提交',
                    handler: function () {
                        var form = this.up('form').getForm();
                        if (form.isValid()) {
                            form.submit({
                                url: 'internalEval/updateEval',
                                success: function (form, action) {
                                    Ext.Msg.alert('', '修改成功', function () {
                                        form.owner.ownerCt.close();
                                        Ext.getCmp('internalEvalMapperWindow').query('grid')[0].store.reload();
                                    }, this);
                                },
                                failure: function (form, action) {
                                    var res = Ext.decode(action.response.responseText);
                                    Ext.Msg.alert('', res.failure);
                                }
                            });
                        }
                    }
                }]
            }]
        });
        win.query('form')[0].getForm().setValues(data);
        win.show();
    },

    remove: function () {
        var row = this.ownerCt.ownerCt.query('grid')[0].getSelectionModel().selected.items;
        if (row.length == 0) {
            Ext.Msg.alert('', '需要选中一行记录');
            return;
        }
        var id = row[0].data.id;
        Ext.MessageBox.confirm('', '您确定要删除吗?', function (opt) {
            if (opt == 'yes') {
                Ext.Ajax.request({
                    url: 'internalEval/deleteEval',
                    params: {id: id},
                    success: function (response) {
                        var res = Ext.decode(response.responseText);
                        if (res.success) {
                            Ext.Msg.alert('删除成功', '删除成功', function () {
                                Ext.getCmp('internalEvalMapperWindow').query('grid')[0].store.reload();
                            }, this);
                        } else {
                            Ext.Msg.alert('操作失败', res.failure);
                        }
                    },
                    failure: function (action) {
                        Ext.Msg.alert('', '操作信息失败');
                    }
                });
            }
        })
    },
    refresh: function () {
        this.ownerCt.ownerCt.query('grid')[0].store.reload();
    }
});


